<?php
//判断参数是否为空
function isArgumentEmpty($argName){
	$args = !$_POST ? $_GET : $_POST;
	return (!isset($args[$argName]) || !$args[$argName]);
}

//格式化接口返回值为json
function ajaxReturn($status=0,$msg='',$data=array()){
	$result = array(
	    'status' => $status,
	    'msg' => $msg,
	    'data' => $data,
	);
	echo(json_encode($result));
}

//将字符串处理成md5
//function getMD5String($str){
//	return md5(C('MD5_PREFIX').$str);
//}

//开发者调试方法
function cDebug($data){
	echo "<pre>";
    	var_dump($data);exit;
    }
    
//将type字段转换成用户友好的数据
function getDataType($type){
	return $type == 0 ? "未匹配" : "已完成";
}

//将status字段转换成用户友好的数据
function getDataStatus($status){
	if ($status == -1){
		return '删除';
	}
	if ($status == 0){
		return '关闭';
	}
	if ($status == 1){
		return '开启';
	}
}

//根据当前页面的controller，设置侧面菜单选中状态
function getMenuActiveStatus($controller){
	$currentC = strtolower(CONTROLLER_NAME);
	if ($currentC == strtolower($controller)){
		return 'class="active"';
	}
} 


//生成uuid方法
 function get_uuid() {
			if (function_exists('com_create_guid')) {
				return com_create_guid();
			} else {
				mt_srand((double) microtime()*10000);//optional for php 4.2.0 and up.
				$charid = strtoupper(md5(uniqid(rand(), true)));
				$hyphen = chr(45);// "-"
				$uuid   = chr(123)// "{"
				 .substr($charid, 0, 8).$hyphen
				.substr($charid, 8, 4).$hyphen
				.substr($charid, 12, 4).$hyphen
				.substr($charid, 16, 4).$hyphen
				.substr($charid, 20, 12)
				.chr(125);// "}"
				return $uuid;
			}
		}

//根据角色id得到角色名
 function getRoleById($admin_role_id){
 	$role = D('admin_info')->find($admin_role_id);
 	$roleName = $role['admin_name'];
 	return $roleName;
 }
 //根据出生日期得到年龄
 function birthday($birthday){ 
	 $age = strtotime($birthday); 
	 if($age === false){ 
	  	return false; 
	 } 
	 list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); 
	 $now = strtotime("now"); 
	 list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); 
	 $age = $y2 - $y1; 
	 if((int)($m2.$d2) < (int)($m1.$d1)) 
	  $age -= 1; 
	 return $age; 
} 
function getPath($page){
	if(!session('admin')){
	//	var_dump($page);
		$page->redirect('Login/index');
	}
}
function get_week($date){
    //强制转换日期格式
//  $date_str=date('Y-m-d',$date);

    //封装成数组
    $arr=explode("-", $date);
     
    //参数赋值
    //年
    $year=$arr[0];
     
    //月，输出2位整型，不够2位右对齐
    $month=sprintf('%02d',$arr[1]);
     
    //日，输出2位整型，不够2位右对齐
    $day=sprintf('%02d',$arr[2]);
     
    //时分秒默认赋值为0；
    $hour = $minute = $second = 0;   
     
    //转换成时间戳
    $strap = mktime($hour,$minute,$second,$month,$day,$year);
     
    //获取数字型星期几
    $number_wk=date("w",$strap);
     
    //自定义星期数组
    $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
     
    //获取数字对应的星期
    return $weekArr[$number_wk];
}

function get_allDate($start,$end){
	$dateArray = [];
    $start_number = strtotime($start);
    $end_number = strtotime($end);
    for($i=$start_number; $i<=$end_number;$i+=(24*3600)){
        array_push($dateArray,date("Y-m-d",$i));
    }
    return $dateArray;
 
}
    /**
     * 通用函数 传两个值 相等返回time-on or 空
     */
    function isTimeon($v1, $v2){
        if($v1 == $v2){
            return 'time-on';
        }else{
            return '';
        }
    }
    /**
     * 通用函数 传两个值 相等返回mui-active or 空
     */
    function isMuiactive($v1, $v2){
        if($v1 == $v2){
            return 'mui-active';
        }else{
            return '';
        }
    }
    /**
     * 通用函数 传两个值 disabled="true" or 空
     */
    function isDisabled($v1, $v2){
        if($v1 == $v2){
            return 'disabled="true"';
        }else{
            return '';
        }
    }
    /**
     * 通用函数 传两个值 checked="" or 空
     */
    function isCheck($v1, $v2){
        if($v1 == $v2){
            return 'checked=""';
        }else{
            return '';
        }
    }
    function isHourM($v1){
        $hour = floor($v1/60);
        if($hour>0){
            $hour.='小时';
        }else{
            $hour = '';
        }
        $minute = floor(($v1-60*$hour)%60);
        if($minute>0){
            $minute.='分钟';
        }else{
            $minute = '';
        }
        return $hour.$minute;
    }

    class IndexCommon {
    /**
     * 发送HTTP请求方法
     * @param  string $url    请求URL
     * @param  array  $params 请求参数
     * @param  string $method 请求方法GET/POST
     * @return array  $data   响应数据
     */
    public static function sendCurl($url, $params, $method = 'GET', $header = array(), $multi = false){
        $data['errorCode'] = 1;
        $data['msg'] = "";
        $data['data'] = array();
        
        $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
        );
        /* 根据请求类型设置特定参数 */
        switch(strtoupper($method)){
            case 'GET':
                $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
                break;
            case 'POST':
                //判断是否传输文件
                $params = $multi ? $params : http_build_query($params);
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
            case 'POST_JSON':
                //判断是否传输文件
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                $opts[CURLOPT_HTTPHEADER] = array(
                                                'Content-Type: application/json',
                                                'Content-Length: ' . strlen($params));
                
                break;
            default:
                $data['result'] = 0;
                $data['msg'] = "不支持的请求方式！";
                return $data;
        }
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        if ($method == 'POST_JSON') {
            $curlData = json_decode(curl_exec($ch), true);
        } else {
            $curlData = json_decode(curl_exec($ch), true);
        }
        curl_close($ch);
        if(isset($curlData['status']) && !$curlData['status']) {
            $data['errorCode'] = 0;
            $data['msg'] = '请求发生错误：' . $curlData['notes'];
            return $data;
        }
        $data['data'] = $curlData;
        return  $data;
    }
}
    function get_orderStatus($rs){
        if ($rs == 20){
            return '待发货';
        }else if($rs == 30){
            return '已发货';
        }else if($rs == 40){
            return '已完成';
        }else if($rs == 50){
            return '已退款';
        }
    }

    function get_accountStatus($rs){
        if ($rs == 0){
            return '未结算';
        }else if($rs == 1){
            return '已结算';
        }
    }

    function get_userName($rs){
        if (strlen($rs) == 11){
            $rs = substr($rs, 0, 3).'****'.substr($rs, 7);
        }

        return $rs;
    }

    function getWeekdays($day){
        $lastday=date('Y-m-d',strtotime("$day Sunday"));
        $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
        return array($firstday,$lastday);
    }

function getMonthdays($day){
    $firstday = date('Y-m-01',strtotime($day));
    $lastday = date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    return array($firstday,$lastday);
}

?>